﻿// *******************************************************************************************************
// Example code to show how to read and write to a 24C02 EEPROM using the Devantech USB-I2C Module.
// Writen in Visual C# Express 2008.
// Before using this code you must have installed the FTDI VCP drivers. 
// The VCP drivers make the USB-I2C appear as a serial Port on your PC.
// See http://www.robot-electronics.co.uk/htm/usb_i2c_tech.htm
// NOTE!   This code contains no error handling. 
//         It's point is to show how to use C#, the USB-I2C and an EEPROM.
// *******************************************************************************************************
// The 24C02 uses a 1-byte internal register address
// *******************************************************************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace EE24C02
{
     public partial class EE24C02 : Form
    {
       static SerialPort USB_I2C;

        public EE24C02()
        {
           InitializeComponent();

            USB_I2C = new SerialPort();
            // Get a list of serial port names.
            foreach (string s in SerialPort.GetPortNames())
            {
                Ports.Items.Add(s);
            }
            Message.Text = "Select Com port";
        }

        private void WriteButton_Click(object sender, EventArgs e)
        {
            byte[] SerBuf = new byte[20];
            byte Addr;

            Addr = Convert.ToByte(WriteAddr.Text, 16);
            for (int x = 0; x < 8; x++)
            {
                SerBuf[x + 4] = Convert.ToByte(WriteData.Text.Substring(x * 3, 2), 16);
            }
            SerBuf[0] = 0x55;               // Command for Devices with 1 address byte 
            SerBuf[1] = 0xA0;               // 24C02 I2C address
            SerBuf[2] = Addr;               // 24C02 internal address - must be on 8 byte boundry for EEPROM page write operation
            SerBuf[3] = 8;                  // number of bytes to write
            USB_I2C.Write(SerBuf, 0, 12);   // send the write command and data to the USB-I2C module
            while(USB_I2C.BytesToRead<1);   // wait for result of write operation to arrive
            USB_I2C.Read(SerBuf, 0, 1);     // get the result of write operation
            ReadEEPROM();
            if(SerBuf[0]==0) {
                Message.Text = "EEPROM Write Failed";
            }
            else {
                Message.Text = "EEPROM Write Succeeded";
            }
         }

        private void ReadButton_Click(object sender, EventArgs e)
        {
            ReadEEPROM();
        }

        private void Ports_SelectedIndexChanged(object sender, EventArgs e)
        {
            byte[] SerBuf = new byte[10];
            USB_I2C.PortName = Ports.Text;
            USB_I2C.Parity = 0;
            USB_I2C.BaudRate = 19200;
            USB_I2C.StopBits = StopBits.Two;
            USB_I2C.DataBits = 8;
            USB_I2C.ReadTimeout = 500;
            USB_I2C.WriteTimeout = 500;
            USB_I2C.Open();
            SerBuf[0] = 0x5A;    // USB_I2C Module Command
            SerBuf[1] = 0x01;     // Get Revision
            SerBuf[2] = 0;
            SerBuf[3] = 0;
            USB_I2C.Write(SerBuf, 0, 4);
            USB_I2C.Read(SerBuf, 0, 1);
            USB_I2C_VER.Text = string.Format("{0}", SerBuf[0]);
            ReadAddr.Text = "00";
            WriteAddr.Text = "00";
            ReadEEPROM();
        }

        private void ReadEEPROM()
        {
            byte[] SerBuf = new byte[10];
            byte Addr;

            Addr = Convert.ToByte(ReadAddr.Text, 16);
            SerBuf[0] = 0x55;                    // Command for Devices with 1 address byte 
            SerBuf[1] = 0xA1;                    // 24C02 I2C address + low bit set to indicate its a read operation
            SerBuf[2] = Addr;                    // 24C02 internal address to read from
            SerBuf[3] = 8;                       // number of bytes to read
            USB_I2C.Write(SerBuf, 0, 4);         // send the read command to the USB-I2C module
            while(USB_I2C.BytesToRead < 8);      // wait for data to arrive from the EEPROM
            USB_I2C.Read(SerBuf, 0, 8);          // get the 8 data bytes
            ReadData.Text = string.Format("{0:X02} {1:X02} {2:X02} {3:X02} {4:X02} {5:X02} {6:X02} {7:X02}", SerBuf[0], SerBuf[1], SerBuf[2], SerBuf[3], SerBuf[4], SerBuf[5], SerBuf[6], SerBuf[7]);
            Message.Text = "EEPROM Read";
        }
    }
}
